<?php
/************************************
*   新浪相册 7.0
*   @file:page_controllor.php
*   @intro:页面控制器
*   @author:shaopu@
*   @date:Sun Sep 20 17:35:43 CST 2009
************************************/

class page_controllor extends controllor 
{
    //页面需要的数据
    private   $__page_data = array();   
    protected $__page_id;
    protected $__page_title;
    private   $__set_domain = false;    
    public    $__lang;
    protected $__isLangEng = false;
    public    $__manage;                //是否管理页面
    
    
    protected $__js_pagedata = array();
    
    protected $page = '';      //页面标识，用于在导航上进行页面签区分
    
    

    public function __construct($get_param)
    {
        parent::controllor($get_param);
        
        //语言
        if(in_array($_COOKIE[K_COOKIE_LANG] , array(K_LANG_CHN , K_LANG_ENG)))
        {
            $this->__isLangEng = $_COOKIE[K_COOKIE_LANG] == K_LANG_ENG ? true : false;
            $this->__lang = K::config($_COOKIE[K_COOKIE_LANG]);
        }
        else 
        {
            $this->__isLangEng = K_DEFAULT_LANG == K_LANG_ENG ? true : false;
            $this->__lang = K::config(K_DEFAULT_LANG);
        }
                        
        
        //默认的PAGEID就是当前实例的类名 不要前面的p_
        if(!$this->__page_id)
            $this->__page_id = get_class($this);
            
        //初始化SESSION
        if($this->__need_session)
        {
            $this->init_session();
            
            if($this->__need_login && !$this->login)
            {
                $this->redirect('/login/'.base64_encode($_SERVER['REQUEST_URI']));
            }
        }
        
        K::set_controllor($this);
        
        $this->construct();
		$this->run();
    }
    
    /**
     * 设置当前页面的PAGEID，默认为当前页面程序去掉.php
     *
     * @param string $sPageId
     * @return true
     */
    public function set_page_id($sPageId)
    {
        $this->__page_id = $sPageId;
        return true;
    }
    public function set_page_title($sPageTitle)
    {
        $this->__page_title = $sPageTitle;
    }
    public function set_domain($bool)
    {
        $this->__set_domain = $bool;
    }
    public function add_js_pagedata($key , $val)
    {
        $this->__js_pagedata[$key] = $val;
    }
    /**
     * 添加页面数据
     *
     * @param string $key   数据名称
     * @param mix $value    数据值
     */
    public function set_pagedata($key , $value)
    {
        $this->__page_data[$key] = $value;
        return ;
    }
    /**
     * 追加某页面数据数组
     * 当前页面数据如果不存在，则创建
     *
     * @param string $key
     * @param mix $value
     * @param string $subkey
     */
    public function append_pagedata($key , $value , $subkey = null)
    {
        if(!isset($this->__page_data[$key]))
            $this->__page_data[$key] = array();
        
        if(!is_array($this->__page_data[$key]))
            K::halt(__CLASS__.' '.__FUNCTION__.' not_array '.$key);
            
        $this->__page_data[$key][$subkey] = $value;
            
        return ;
    }
    /**
     * 加载页面组件
     *
     * @param string $comp_name
     * @param array $para
     * @return object
     */
    protected function load_component($comp_name , $para = null)
    {
        $classname = 'comp_'.$comp_name;
            
        $obj = new $classname($para);
        return $obj;
    }
    
    protected function load_notice()
    {
    	$notice = K::config('notice_data');
    	if($this->__isLangEng)
    		$notice['notice'] = nl2br($notice['notice_eng']);
    	else
    		$notice['notice'] = nl2br($notice['notice']);
    	return $notice;
    }
    
    private function _init_scope()
    {
        $data = array(
            'page_id' => $this->__page_id,
            'visitor' => array(
                'uid'=> $this->visitor['uid'],
                'nick'=> K_str::un_html($this->visitor['nick'])
            ),
            
        );
        $data['page_data'] = array();
        if($this->__js_pagedata)
            $data['page_data'] = $this->__js_pagedata;
        return json_encode($data);
    }

    /**
     * 输出页面
     *
     * @param string $tpl   模板名称
     * @param array $data
     */
    protected function output($tpl , $data = array())
    {
        $this->_header_no_cache();
        
        //合并页面数据
        $this->__page_data = array_merge($data , $this->__page_data);
        $_tpl_file_path_ = K5_TPL_PATH.'/tpl_'.$tpl.'.php';
        unset($tpl);
        
        //基本数据
        $visitor = $this->visitor;
		$myself = $this->myself;
		$islogin = $this->login;
		$page_id= $this->__page_id;
		$page_title = $this->__page_title;
		$is_lang_eng = $this->__isLangEng;
        //初始化页面SCOPE数据
        $scope = $this->_init_scope();
        
        
        //把页面数据数组变为单独变量，方便页面调用
        extract($this->__page_data);
        
        
        $L = $this->__lang;
        //读入模板程序
        
        include($_tpl_file_path_);
        
        //一些统计
        $this->_over();
        die;
    }
    
    /**
     * 跳转到系统繁忙页
     *
     */
    protected function busy()
    {
        die('busy');
    	$this->redirect('/page/notfound.php');
		
    	//TODO change this thing into system busy page?
    	$this->__page_id = 'notfound';
        $_tpl_file_path_ = K5_TPL_PATH.'/tpl_notfound.php';
    	include($_tpl_file_path_);
        //一些统计
        $this->_over();
        die();
    }
    protected function not_found()
    {
        die('not found');
    	$this->redirect('/page/notfound.php');
		
    	//TODO change this thing into system busy page?
    	$this->__page_id = 'notfound';
        $_tpl_file_path_ = K5_TPL_PATH.'/tpl_notfound.php';
    	include($_tpl_file_path_);
        //一些统计
        $this->_over();
        die();
    }

    protected function add_history($type , $data)
    {
        if($this->login)
        {
            $history = unserialize($this->visitor['history']);
            $history[$type][] = $data;
            $history[$type] = array_slice(array_unique($history[$type]) , 0 ,10);
            $oMember = K::load_mod('member');
            $oMember->edit_value($this->visitor['uid'] , 'history' , serialize($history));
        }
        else 
        {
            $oSess = K::session();
            $oSess->get('history' , $history);
            $history[$type][] = $data;
            $history[$type] = array_slice(array_unique($history[$type]) , 0 , 10);
            $oSess->set('history' , $history);
        }
    }
}
?>